package com.qh.cqac.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qh.cqac.entity.IAS;
import com.qh.cqac.entity.Instructor;
import com.qh.cqac.entity.pojo.Ins;
import com.qh.cqac.mapper.IasMapper;
import com.qh.cqac.mapper.InstructorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static java.util.Comparator.comparing;

@Controller
public class IndexController {

    @Autowired
    private IasMapper iasMapper;

    @Autowired
    private InstructorMapper instructorMapper;

    @RequestMapping("/")
    public String hello(){
        return "display1";
    }


    @ResponseBody
    @RequestMapping("/getScores")
    public List<Instructor> getScores(Integer type){
//        System.out.println("type"+type);
        QueryWrapper<Instructor> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        QueryWrapper<IAS> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("type",type);
        //查找到所有辅导员的信息
        List<Instructor> instructorList = instructorMapper.selectList(null);
        List<Instructor> instructorList2 = instructorMapper.selectList(wrapper);
        //查找到所有第 type 轮打分的信息
        List<IAS> num = iasMapper.selectList(wrapper2);
        //获取到 type 轮每个老师获取分数的平均值
        List<Ins> list = iasMapper.findAvg(type);

        if(type==1){//如果是第一轮
            if(
                    instructorList.get(0).getScore()==null||instructorList.get(0).getScore()==0||
                    instructorList.get(0).getScore1()==null||instructorList.get(0).getScore1()==0||
                    instructorList.get(0).getPm()==null||instructorList.get(0).getPm()==0
            ){//如果第一轮的打分成绩或第一轮总成绩 为空或者为0
                if(num==null||num.isEmpty()||num.size()!=100){//如果没有打分或者打分的数据量不足
                    return instructorMapper.selectList(null);
                }else {//如果打分数据可以使用
                    for(Ins ins :list) {
                        //根据每个ins（导员id，导员平均成绩） 查找到导员
                        Instructor instructor = instructorMapper.selectById(ins.getId());
                        //将案例分析/谈心谈话分数赋值给导员
                        instructor.setScore(ins.getAvg());
                        //计算第一轮总分数
                        Double score = instructor.getFirst() * 0.45 +
                                instructor.getWebs() * 0.05 +
                                ins.getAvg() * 0.5;
                        //将第一轮总分数赋值给导员
                        BigDecimal bg = new BigDecimal(String.valueOf(score)).setScale(2, RoundingMode.HALF_UP);
                        instructor.setScore1(bg.doubleValue());
                        //更新数据
                        instructorMapper.updateById(instructor);
                    }
                    //重新获取到所有导员数据
                    List<Instructor> instructors = instructorMapper.selectList(null);
                    //按照第一轮分数排序
                    instructors.sort(new Comparator<Instructor>() {
                        @Override
                        public int compare(Instructor o1, Instructor o2) {
                            if(o1.getScore1()>o2.getScore1()){
                                return -1;
                            }else if (o1.getScore1()<o2.getScore1()) {
                                return 1;
                            }else {
                                if(o1.getScore()>o2.getScore()){
                                    return -1;
                                }else if (o1.getScore()<o2.getScore()){
                                    return 1;
                                }else {
                                    if (o1.getFirst()>o2.getFirst()){
                                        return -1;
                                    }else if(o1.getFirst()>o2.getFirst()){
                                        return 1;
                                    }else {
                                        if (o1.getWebs()==o2.getWebs())
                                            return 0;
                                        return o1.getWebs()>o2.getWebs()?-1:1;
                                    }
                                }
                            }
                        }
                    });
                    //根据成绩给导员排名，并逐条更新导员数据
                    int j = 1; Instructor i2 = null;
                    for(Instructor i:instructors){
                        if(j<=10){
                            i.setType(2);
                        }
                        i.setPm(j++);
                        instructorMapper.updateById(i);
                    }
                    return instructors;
                }
            }else {//如果第一轮数据都有的，直接查出来按照分数排序后输出
                for (Instructor i:instructorList){
                    if(
                            i.getPw11()==null||i.getPw11()==0.0||
                            i.getPw12()==null||i.getPw12()==0.0||
                            i.getPw13()==null||i.getPw13()==0.0||
                            i.getPw14()==null||i.getPw14()==0.0||
                            i.getPw15()==null||i.getPw15()==0.0
                    ){
                        for(IAS ias: num){
                            Instructor instructor = instructorMapper.selectById(ias.getIid());
                            String jname = ias.getJname();
                            switch (jname){
                                case "1":
                                    instructor.setPw11(ias.getScore());
                                    break;
                                case "2":
                                    instructor.setPw12(ias.getScore());
                                    break;
                                case "3":
                                    instructor.setPw13(ias.getScore());
                                    break;
                                case "4":
                                    instructor.setPw14(ias.getScore());
                                    break;
                                case "5":
                                    instructor.setPw15(ias.getScore());
                                    break;
                            }
                            instructorMapper.updateById(instructor);
                        }
                        break;
                    }
                }
                instructorList.sort(comparing(Instructor::getPm));//.reversed()
                return instructorList;
            }
        }else {//如果是第二轮
            if(instructorList2.get(0).getTalk()==null||instructorList2.get(0).getTalk()==0
                ||instructorList2.get(0).getScore2()==null||instructorList2.get(0).getScore2()==0
                ||instructorList2.get(0).getPm2()==null||instructorList2.get(0).getPm2()==0
            ){ //如果第二轮的宣讲对谈成绩或总分  为空或为0
                if(num==null||num.isEmpty()||num.size()!=50){//如果打分数据不合格
                    if (instructorList2==null||instructorList2.isEmpty())
                        return null;
                    instructorList2.sort(comparing(Instructor::getPm).reversed());
                    return instructorList2;
                }else {//如果打分数据合格，遍历分数
                    for(Ins ins :list) {
                        //查找到导员
                        Instructor instructor = instructorMapper.selectById(ins.getId());
                        //将宣讲对谈成绩赋值
                        instructor.setTalk(ins.getAvg());
                        Double score =
                                instructor.getScore1() * 0.5 +
                                ins.getAvg() * 0.5;
                        //将第二轮成绩赋值
                        BigDecimal bg = new BigDecimal(String.valueOf(score)).setScale(2, RoundingMode.HALF_UP);
                        instructor.setScore2(bg.doubleValue());
                        //更新导员数据
                        instructorMapper.updateById(instructor);
                    }
                    //重新获取数据
                    List<Instructor> instructors = instructorMapper.selectList(wrapper);
                    instructors.sort(new Comparator<Instructor>() {
                        @Override
                        public int compare(Instructor o1, Instructor o2) {
                            if(o1.getScore2()!=o2.getScore2()){
                                return o1.getScore2()>o2.getScore2()?-1:1;
                            }else{
                                if(o1.getScore1()!=o2.getScore1()){
                                    return o1.getScore1()>o2.getScore1()?-1:1;
                                }
                                else {
                                    if (o1.getTalk()!=o2.getTalk()){
                                        return o1.getTalk()>o2.getTalk()?-1:1;
                                    }else {
                                        return 0;
                                    }
                                }
                            }
                        }
                    });
                    for (Instructor i:instructorList2){
                        if(
                                i.getPw21()==null||i.getPw21()==0.0||
                                        i.getPw22()==null||i.getPw22()==0.0||
                                        i.getPw23()==null||i.getPw23()==0.0||
                                        i.getPw24()==null||i.getPw24()==0.0||
                                        i.getPw25()==null||i.getPw25()==0.0
                        ){
//                            System.out.println(i.getName());
                            for(IAS ias: num) {
                                if (ias.getIid() == i.getId()) {
                                    String jname = ias.getJname();
                                    switch (jname) {
                                        case "1":
                                            i.setPw21(ias.getScore());
                                            break;
                                        case "2":
                                            i.setPw22(ias.getScore());
                                            break;
                                        case "3":
                                            i.setPw23(ias.getScore());
                                            break;
                                        case "4":
                                            i.setPw24(ias.getScore());
                                            break;
                                        case "5":
                                            i.setPw25(ias.getScore());
                                            break;
                                    }
                                    instructorMapper.updateById(i);
                                }
                            }
                        }
                    }
                    int j = 1;
                    for(Instructor i:instructors){
                        if(j>10)
                            break;
                        i.setPm2(j++);
                        i.setType(2);
                        instructorMapper.updateById(i);
                    }

                    return instructors;
                }
            }else {
                for (Instructor i:instructorList2){
                    if(
                        i.getPw21()==null||i.getPw21()==0.0||
                        i.getPw22()==null||i.getPw22()==0.0||
                        i.getPw23()==null||i.getPw23()==0.0||
                        i.getPw24()==null||i.getPw24()==0.0||
                        i.getPw25()==null||i.getPw25()==0.0
                    ){
                        for(IAS ias: num) {
                            if (ias.getIid() == i.getId()) {
                                String jname = ias.getJname();
                                switch (jname) {
                                    case "1":
                                        i.setPw21(ias.getScore());
                                        break;
                                    case "2":
                                        i.setPw22(ias.getScore());
                                        break;
                                    case "3":
                                        i.setPw23(ias.getScore());
                                        break;
                                    case "4":
                                        i.setPw24(ias.getScore());
                                        break;
                                    case "5":
                                        i.setPw25(ias.getScore());
                                        break;
                                }
                                instructorMapper.updateById(i);
                            }
                        }
                    }
                }
                instructorList2.sort(comparing(Instructor::getPm2));
                return instructorList2;
            }
        }
    }



    //评委打分
    @RequestMapping("/send")
    @ResponseBody
    public String send(Double[] pf,Integer[] ids,Integer type, HttpSession session){
        String jname = (String) session.getAttribute("name");
        QueryWrapper<IAS> wrapper0 = new QueryWrapper<>();
        wrapper0.eq("jname",jname);
        wrapper0.eq("type",type);
        List<IAS> list = iasMapper.selectList(wrapper0);
        if(list!=null&&!list.isEmpty()){
            return "error";
        }else {
            if(type==1){
                for (int i=0;i<20;i++){
                    iasMapper.insert(new IAS(ids[i],pf[i],jname,type));
                    Instructor instructor = instructorMapper.selectById(ids[i]);
                    switch (jname){
                        case "1":
                            if(instructor.getPw11()==null||instructor.getPw11()==0.0){
                                instructor.setPw11(pf[i]);
                            }
                            break;
                        case "2":
                            if(instructor.getPw12()==null||instructor.getPw12()==0.0){
                                instructor.setPw12(pf[i]);
                            }
                            break;
                        case "3":
                            if(instructor.getPw13()==null||instructor.getPw13()==0.0){
                                instructor.setPw13(pf[i]);
                            }
                            break;
                        case "4":
                            if(instructor.getPw14()==null||instructor.getPw14()==0.0){
                                instructor.setPw14(pf[i]);
                            }
                            break;
                        case "5":
                            if(instructor.getPw15()==null||instructor.getPw15()==0.0){
                                instructor.setPw15(pf[i]);
                            }
                            break;
                    }
                   instructorMapper.updateById(instructor);
                }
            }else {
                for (int i=0;i<10;i++){
                    iasMapper.insert(new IAS(ids[i],pf[i],jname,type));
                    Instructor instructor = instructorMapper.selectById(ids[i]);
                    switch (jname){
                        case "1":
                            if(instructor.getPw21()==null||instructor.getPw21()==0.0){
                                instructor.setPw21(pf[i]);
                            }
                            break;
                        case "2":
                            if(instructor.getPw22()==null||instructor.getPw22()==0.0){
                                instructor.setPw22(pf[i]);
                            }
                            break;
                        case "3":
                            if(instructor.getPw23()==null||instructor.getPw23()==0.0){
                                instructor.setPw23(pf[i]);
                            }
                            break;
                        case "4":
                            if(instructor.getPw24()==null||instructor.getPw24()==0.0){
                                instructor.setPw24(pf[i]);
                            }
                            break;
                        case "5":
                            if(instructor.getPw25()==null||instructor.getPw25()==0.0){
                                instructor.setPw25(pf[i]);
                            }
                            break;
                    }
                    instructorMapper.updateById(instructor);
                }
            }
            return "success";
        }

    }

    @RequestMapping("/login")
    public String login1(Model model,String name,Integer type, HttpSession session){
        session.setAttribute("name",name);
        QueryWrapper<Instructor> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        if(type==1){
            model.addAttribute("list",instructorMapper.selectList(null));
        }else{
            List<Instructor> list = instructorMapper.selectList(wrapper);
            list.sort(comparing(Instructor::getScore1));
            model.addAttribute("list",list);
        }

        model.addAttribute("type",type);
        return "index";
    }
}
